var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var userUrl = require("url/user");
var helperOpen = require("helper/open");
var bankUrl = userUrl.url.bank.managePage;
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var noData = [ {
	"template" : 1
} ];
// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var listView=ui("listView");

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");

header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "银行卡",
	"labelNext" : "添加"
})
header.refreshData();
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(data) {
	loadPage();
});
listView.bindItems(listdata);
do_Page.on("result", function(data) {
	if(data&&data.fromPage&&data.fromPage==="bank_add"){
		loadPage();
	}
});
//刷新数据
function loadPage() {
	qqHttp.post(bankUrl, {}, function(data) {
		listView.rebound();
		listdata.removeAll();
		if(data.page.content.length>0){
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
var bankVo;
listView.on("touch", function(index) {
	for (var i = 0; i < listdata.getCount(); i++) {
		var d = listdata.getOne(i);
		if (index == i){
			bankVo=d;
			d.source = "source://image/common/radiocheck.png";
		}else{
			d.source = "source://image/common/radio.png";
		}
		listdata.updateOne(i, d);
	}
	listView.refreshItems();
});

headerRight.on("touch", function() {
	helperOpen.open("source://view/user/bank/add/main.ui");
})
ui("do_Button_1").on("touch", function() {
	if(null==bankVo){
		nf.toast("请选择一张银行卡");
	}else{
		do_App.closePage({fromPage:'bank_select',"bankVo":bankVo})
	}
})
